package com.masterlu.module.product.service.liked;

import com.masterlu.framework.common.pojo.PageResult;
import com.masterlu.module.product.controller.admin.liked.vo.ProductLikedPageReqVO;
import com.masterlu.module.product.controller.app.liked.vo.AppLikedPageReqVO;
import com.masterlu.module.product.convert.liked.ProductLikedConvert;
import com.masterlu.module.product.dal.dataobject.liked.ProductLikedDO;
import com.masterlu.module.product.dal.mysql.liked.ProductLikedMapper;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static com.masterlu.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.masterlu.module.product.enums.ErrorCodeConstants.LIKED_EXISTS;
import static com.masterlu.module.product.enums.ErrorCodeConstants.LIKED_NOT_EXISTS;

/**
 * 商品收藏 Service 实现类
 *
 * @author jason
 */
@Service
@Validated
public class ProductLikedServiceImpl implements ProductLikedService {

    @Resource
    private ProductLikedMapper productLikedMapper;

    @Override
    public Long createLiked(Long userId, Long commentId) {
        ProductLikedDO Liked = productLikedMapper.selectByUserIdAndCommentId(userId, commentId);
        if (Liked != null) {
            throw exception(LIKED_EXISTS);
        }

        ProductLikedDO entity = ProductLikedConvert.INSTANCE.convert(userId, commentId);
        productLikedMapper.insert(entity);
        return entity.getId();
    }
    @Override
    public Long createOrUpdateLiked(Long userId, Long commentId,Boolean liked) {

        ProductLikedDO productLikedDO = productLikedMapper.selectByUserIdAndCommentId(userId, commentId);
        if (productLikedDO != null) {/*存在则更新*/
            productLikedDO.setDeleted(!liked);
            // 更新
            productLikedMapper.updateById(productLikedDO);
            return  productLikedDO.getId();
        }else{/*不存在则插入*/
            ProductLikedDO entity = ProductLikedConvert.INSTANCE.convert2(userId, commentId,!liked);
            productLikedMapper.insert(entity);
            return entity.getId();
        }
    }

    @Override
    public void deleteLiked(Long userId, Long spuId) {
        ProductLikedDO Liked = productLikedMapper.selectByUserIdAndCommentId(userId, spuId);
        if (Liked == null) {
            throw exception(LIKED_NOT_EXISTS);
        }

        productLikedMapper.deleteById(Liked.getId());
    }

    @Override
    public PageResult<ProductLikedDO> getLikedPage(Long userId, @Valid AppLikedPageReqVO reqVO) {
        return productLikedMapper.selectPageByUserAndType(userId, reqVO);
    }

    @Override
    public PageResult<ProductLikedDO> getLikedPage(@Valid ProductLikedPageReqVO reqVO) {
        return productLikedMapper.selectPageByUserId(reqVO);
    }

    @Override
    public ProductLikedDO getLiked(Long userId, Long commentId) {
        return productLikedMapper.selectByUserIdAndCommentId(userId, commentId);
    }

    @Override
    public Long getLikedCount(Long userId) {
        return productLikedMapper.selectCountByUserId(userId);
    }

}
